package com.chenliwu.design.patterns.strategy;

import com.chenliwu.design.patterns.strategy.payment.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenliwu
 * @since 2022-06-29
 */
public class StrategyDemo {
    private static List<IPayService> payServices = new ArrayList<>();

    static {
        payServices.add(new AliPayService());
        payServices.add(new WeixinPayService());
        payServices.add(new HuaweiPayService());
    }

    public static void main(String[] args) {
        test1();
    }

    static void test1() {
        // 未使用【策略模式】执行支付判断和处理
        Payment payment = new Payment();
        if ("AliPay".equals(payment.getPayMode())) {
            System.out.println("【支付宝】支付");
            System.out.println("返回支付结果");
        } else if ("WeixinPay".equals(payment.getPayMode())) {
            System.out.println("【微信】支付");
            System.out.println("返回支付结果");
        } else if ("HuaweiPay".equals(payment.getPayMode())) {
            System.out.println("【华为】支付");
            System.out.println("返回支付结果");
        } else if ("xxx".equals(payment.getPayMode())) {
            System.out.println("【xxx】支付");
            System.out.println("返回支付结果");
        }
    }


    static void test2() {
        // 使用【策略模式】执行支付判断和处理
        Payment payment = new Payment();
        for (IPayService payService : payServices) {
            if (payService.support(payment)) {
                payService.handlePayment(payment);
            }
        }
    }
}
